//author:jxx
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
import CampHouseOrderBox from '../../views/mall/CampHouseOrderBox.vue'
let extension = {
    components: {//动态扩充组件或组件路径
        //表单header、content、footer对应位置扩充的组件
        gridHeader: '',//{ template: "<div>扩展组xx件</div>" },
        gridBody: CampHouseOrderBox,
        gridFooter: '',
        //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
        modelHeader: '',
        modelBody: '',
        modelFooter: ''
    },
    buttons: { view: [], box: [], detail: [] },//扩展的按钮
    data: {
        return: {
            merchantCode: '',
            merchantName: '',
            CampItemStatusList: [],
            unitCode: [],
            details: {}
        }
    },
    methods: {//事件扩展
        getFormOption (field) {
            let option;
            this.editFormOptions.forEach(x => {
                x.forEach(item => {
                    if (item.field == field) {
                        option = item;
                    }
                })
            })
            return option;
        },
        getDetailColumns (field) {
            let option;
            this.detail.columns.forEach(item => {
                if (item.field == field) {
                    option = item;
                }
            })
            return option;
        },
        onInit () {
            this.$store.getters.data().viewPageTest = this;
            this.detailOptions.clickEdit = true;
            let houseType = this.getDetailColumns('houseType')
            let type = this.getDetailColumns('type')
            let unLock = this.getDetailColumns('unLock')

            let num = this.getFormOption('num')
            let type2 = this.getFormOption('type')
            let unitId = this.getFormOption('unitId')
            this.http.get('/api/CampHouseUnit/GetCampUnit').then(res => {
                unitId.data = []
                this.merchantCode = res.data.merchantCode
                this.merchantName = res.data.merchantName
                unitId.data = res.data.list.map(i => {
                    return {
                        key: i.id,
                        value: i.name
                    }
                })
                this.unitCode = res.data.list.map(i => {
                    return {
                        id: i.id,
                        code: i.code
                    }
                })
                this.columns.forEach(i => {
                    if (i.field == 'unitId') {
                        i.bind.data = unitId.data
                    }
                })
            })

            type.onChange = (column, row, list) => {
                let code = ''
                if (this.unitCode && this.editFormFields.unitId) {
                    code = this.unitCode.filter(i => { if (i.id == this.editFormFields.unitId) { return i } })[0].code
                }
                let parma = {
                    code: code,
                    type: row.type
                }
                this.http.get("/api/CampHouseType/GetHouseType", parma).then(res => {
                    list.forEach(i => {
                        if (i.code == row.code) {
                            this.$nextTick(() => {
                                row.houseType = ''
                                i.LIST = []
                                i.LIST = res.data.map(j => {
                                    return {
                                        key: j.id,
                                        value: j.name
                                    }
                                })
                            })
                        }
                    })
                })
            }

            type2.onChange = (n, o) => {
                getType()
            }

            unitId.onChange = (n, o) => {
                getType()
            }
            let that = this
            let FXlist = []
            function getType () {
                if (that.unitCode && that.editFormFields.unitId) {
                    let code = that.unitCode.filter(i => { if (i.id == that.editFormFields.unitId) { return i } })[0].code
                    let parma = {
                        code: code,
                        type: that.editFormFields.type
                    }
                    that.http.get("/api/CampHouseType/GetHouseType", parma).then(res => {
                        houseType.bind.data = FXlist = res.data.map(j => {
                            return {
                                key: j.id,
                                value: j.name
                            }
                        })
                    })
                }
            }
            function PrefixInteger (num, length) {
                return (num / Math.pow(10, length)).toFixed(length).substr(2);
            }
            num.onBlur = (n, o) => {
                this.$refs.detail.rowData = []
                for (let i = 0; i < n; i++) {
                    let obj = {
                        houseType: houseType.bind.data[0].key,
                        code: this.editFormFields.prefix + '-' + PrefixInteger(this.$refs.detail.rowData.length + 1, 6),
                        unLock: this.editFormFields.unLock,
                        type: this.editFormFields.type,
                        area: this.editFormFields.area,
                        status: true,
                        depositPrice: this.editFormFields.depositPrice,
                        LIST: FXlist
                    }
                    this.$refs.detail.rowData.push(obj)
                }
            }

            this.detailOptions.buttons[0].onClick = () => {
                let obj = {
                    houseType: houseType.bind.data[0].key,
                    code: this.editFormFields.prefix + '-' + PrefixInteger(this.$refs.detail.rowData.length + 1, 6),
                    unLock: unLock.bind.data[0].key,
                    type: this.editFormFields.type,
                    area: this.editFormFields.area,
                    status: true,
                    depositPrice: this.editFormFields.depositPrice,
                    LIST: FXlist
                }
                this.$refs.detail.rowData.push(obj)
            }

            this.detail.columns.forEach(i => {
                if (i.field == 'status') {
                    i.onChange = (n, o) => {
                        if (this.currentAction == 'update' && !o.status) {
                            this.$refs.gridBody.openBZ(o)
                        }
                    }
                }
            })
        },
        rowClick ({ row, column, event }) {
            this.$refs.table.$refs.table.toggleRowSelection(row);
            return true;
        },
        modelOpenAfter (row) {
            let num = this.getFormOption('num')
            let unLock = this.getFormOption('unLock')
            let area = this.getFormOption('area')
            let depositPrice = this.getFormOption('depositPrice')
            if (this.currentAction == 'Add') {
                num.hidden = false
                unLock.hidden = false
                area.hidden = false
                this.editFormFields.depositPrice = 0
                this.detail.columns.forEach(i => {
                    if (i.field == 'status') {
                        console.log(1111);
                        i.disabled = true
                    }
                })
            } else {
                num.hidden = true
                unLock.hidden = true
                area.hidden = true
                depositPrice.hidden = true
                this.details = row
                this.detail.columns.forEach(i => {
                    if (i.field == 'status') {
                        i.disabled = false
                    }
                })
                this.editFormFields.openDate = [
                    row.openStartDate,
                    row.openEndDate,
                ]
            }
        },
        searchDetailAfter (data) {
            this.$nextTick(() => {
                this.$refs.detail.rowData.forEach(i => {
                    i.type = i.type.toString()
                    i.status = i.status.toString()
                    i.unLock = i.unLock.toString()
                    i.status = i.status == 0 ? true : false
                    i.LIST = []
                    if (this.unitCode && this.editFormFields.unitId) {
                        let code = this.unitCode.filter(i => { if (i.id == this.editFormFields.unitId) { return i } })[0].code
                        let parma = {
                            code: code,
                            type: i.type
                        }
                        this.http.get("/api/CampHouseType/GetHouseType", parma).then(res => {
                            i.LIST = res.data.map(j => {
                                return {
                                    key: j.id.toString(),
                                    value: j.name
                                }
                            })
                            i.houseType = i.houseType.toString()
                        })
                    }
                })
            })
            return true;
        },
        addBefore (formData) {
            this.setNum(formData)
            return true;
        },
        updateBefore (formData) {
            this.setNum(formData)
            return true;
        },
        setNum (formData) {
            formData.mainData.merchantCode = this.merchantCode
            formData.mainData.merchantName = this.merchantName
            formData.mainData.num = this.$refs.detail.rowData.length
            if (this.editFormFields.openDate) {
                formData.mainData.openStartDate = this.editFormFields.openDate[0]
                formData.mainData.openEndDate = this.editFormFields.openDate[1]
            }
            if (formData.detailData.length != 0) {
                formData.detailData.forEach(i => {
                    i.status = i.status ? 0 : 3

                })
            }
        },
        searchDetailBefore (param) {//查询从表表数据前,param查询参数
            param.order = 'asc'
            return true;
        },
    }
};
export default extension;